//
//  DDFileManager.m
//  DongDongWedding
//
//  Created by 谢曦 on 2016/11/16.
//  Copyright © 2016年 gl. All rights reserved.
//

#import "XXFileManager.h"

@implementation XXFileManager
static  NSFileManager *fileManager;

+ (XXFileManager *)sharedManager
{
    static XXFileManager *sharedAccountManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedAccountManagerInstance = [[self alloc] init];
        fileManager =[NSFileManager defaultManager];
    });
    return sharedAccountManagerInstance;
}
+(void)initialize
{
    fileManager =[NSFileManager defaultManager];

}

/**
 
 */
+(void)createFileWithFileType:(XXFileType)fileType fileName:(NSString *)fileName
{
    
    NSString *filePath= [self filePathWithType:fileType file:fileName];

      if (![fileManager fileExistsAtPath:filePath]) {
        
        [fileManager createFileAtPath:filePath contents:nil attributes:nil];
        
    }


}

/**
 创建文件夹

 */
+(void)createFolderWithType:(XXFileType)fileType FolderName:(NSString *)folderName
{
    

    NSString *folderPath= [self filePathWithType:fileType file:folderName];
  
      if (![fileManager fileExistsAtPath:folderPath]) {
        
        [fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
        
    }

}


+(void)deleteDocumentWithDocumentName:(NSString *)documentname
{
    
    NSString *document=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *folder =[document stringByAppendingPathComponent:documentname];
    BOOL bRet = [fileManager fileExistsAtPath:folder];
    if (bRet) {
        //
        NSError *err;
        [fileManager removeItemAtPath:folder error:&err];
        ;
    }


}



+(NSString *)filePathWithType:(XXFileType)fileType file:(NSString *)fileName{

    NSString *path;
    switch (fileType) {
        case XXFileTypeDocument:
            path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
            break;
        case XXFileTypeCaches:
            path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
            break;
        case XXFileTypeTmp:
            path = NSTemporaryDirectory();
            break;
        default:
            break;
    }
    return   [path stringByAppendingPathComponent:fileName];


}

@end
